﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace Historia.Formulaires
{
    /// <summary>
    /// Interaction logic for frmListePersonnages.xaml
    /// </summary>
    public partial class frmListePersonnages : Window
    {
        #region Membres

        protected DataManager _dm = null;

        #endregion

        #region Constructeur

        public frmListePersonnages()
        {
            InitializeComponent();
        }

        #endregion

        #region Méthodes

        /// <summary>
        /// Show the form in dialog mode
        /// </summary>
        /// <param name="dataManager">DataManager</param>
        /// <returns>bool?</returns>
        public bool? ShowFormDialog(ref DataManager dataManager) {
            this._dm = dataManager;
            this.DisplayData();
            return this.ShowDialog();
        }

        protected void DisplayData() {
            this.lstPersonnages.ItemsSource = this._dm.DB.Tables[DataManager.PERSONNAGES].DefaultView;
        }

        /// <summary>
        /// Affiche le formulaire d'édition d'un personnage
        /// </summary>
        protected void ModifierPersonnage()
        {
            frmPersonnage frm = new frmPersonnage();
            frm.Owner = this;
            frm.ShowFormDialog(ref this._dm, ((DataRowView)this.lstPersonnages.SelectedItem).Row.Field<Int32>("id_personnage"));
        }

        /// <summary>
        /// Affiche le formulaire d'édition d'un personnage
        /// </summary>
        protected void AjouterPersonnage()
        {
            frmPersonnage frm = new frmPersonnage();
            frm.Owner = this;
            frm.ShowFormDialog(ref this._dm, null);
        }

        /// <summary>
        /// Supression d'un personnage
        /// </summary>
        protected void SupprimerPersonnage() {
            ((DataRowView)this.lstPersonnages.SelectedItem).Row.Delete();
            this._dm.DB.AcceptChanges();
        }

        #endregion

        private void lstPersonnages_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (this.lstPersonnages.SelectedItem != null)
                {
                    this.ModifierPersonnage();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnAjouter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.AjouterPersonnage();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnFermer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void btnModifier_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.lstPersonnages.SelectedItem != null)
                {
                    this.ModifierPersonnage();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnSupprimer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Êtes-vous certains de vouloir supprimer ce personnage?","Supprimer",MessageBoxButton.YesNo,MessageBoxImage.Warning,MessageBoxResult.Yes)== MessageBoxResult.Yes)
                {
                    this.SupprimerPersonnage();
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
